#!/bin/bash 

# modsecurty依赖的包：pcre httpd-devel libxml2 apr
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel


#  openresty依赖的包：pcre 、zlib、 openssl
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel git 


# 获取最新版的Modesecurity
git clone https://github.com/SpiderLabs/ModSecurity.git
cd ModSecurity/
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
 make


# 获取最新版的openresty
cd ..
git clone 'https://github.com/openresty/ngx_openresty.git'
cd ngx_openresty
make
cd `find . -type d -name "ngx_openresty*"`


# 在编译standalone后，openresty编译时可以通过"--add-module"添加modsecurity模块：
./configure --prefix=/opt/openresty  --with-pcre-jit  --with-ipv6 --without-http_redis2_module --with-http_iconv_module   -j2 --add-module=../../ModSecurity/n
ginx/modsecurity/ --with-debug
gmake && gmake install 


# OWASP提供的规则是于社区志愿者维护的，被称为核心规则CRS（corerules）,规则可靠强大
cd ../..
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
mv  owasp-modsecurity-crs /opt/openresty/nginx/conf/
cd /opt/openresty/nginx/conf/owasp-modsecurity-crs/ && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
pwd
cd - ;cd ModSecurity/
mv modsecurity.conf-recommended /opt/openresty/nginx/conf/modsecurity.conf
cp unicode.mapping /opt/openresty/nginx/conf/
sed -i 's/^SecRuleEngine.*/SecRuleEngine On/' /opt/openresty/nginx/conf/modsecurity.conf
sed -i '1 iInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' /opt/openresty/nginx/conf/modsecurity.conf
sed -i '1 i#Include your  security rule' /opt/openresty/nginx/conf/modsecurity.conf
echo -e "Don't forget include security rule into modsecurity.conf\n\n"
sleep 3


echo '# Modseurity example
#server {
#      listen      80;
#      server_name test.net www.test.net;
#location ~ \.php$ {
#      ModSecurityEnabled on;  
#      ModSecurityConfig modsecurity.conf;
# 
#      root /web/wordpress;
#      index index.php index.html index.htm;
# 
#      fastcgi_pass   127.0.0.1:9000;
#      fastcgi_index  index.php;
#      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
#      include        fastcgi_params;
#      }
#  }
'>>/opt/openresty/nginx/conf/nginx.conf